<template>
	<ul class="list">
		<li class="item" 
			v-for="item in letters" 
			:key="item" 
			:ref='item'
			@click="handleLetterClick"
			@touchstart.prevent="handleTouchStart"
			@touchmove="handleTouchMove"
			@touchend="handleTouchEnd"
		>{{item}}</li> <!--组织事件的默认行为-->
	</ul>
</template>

<script>
export default {
	name: 'CityAlphabet',
	props: {
		cities: Object
	},
	computed: { 
		letters () {
			const letters = []
			for (let i in this.cities) {
				letters.push(i)
			}
			return letters
		}
	},
	data () {
		return {
				touchStatus: false /*定义标识位*/
		}
	},
	methods: {
		handleLetterClick (e) {/*向父组件触发点击事件，实现字母点击检索效果*/
			this.$emit('chang', e.target.innerText) /*$emit向外触发事件,e.target.innerText获取当前点击的对象*/
		},
		handleTouchStart () {/*实现字母滑动定位效果*/
			this.touchStatus = true
		},
		handleTouchMove (e) { /*实现字母滑动定位效果*/
			if (this.touchStatus) {
				const startY = this.$refs['A'][0].offsetTop  /*offsetTop 可以获取到A离顶部的高度*/
				const touchY = e.touches[0].clientY - 80 /*clientY 获取手指位置离顶部的高度，80指的是顶部蓝色区域的高*/
				const index = Math.floor((touchY - startY) / 20)/*获取手指滑动位置的下标*/
				if (index >= 0 && this.letters.length) {
					this.$emit('chang', this.letters[index])
					//console.log(index)
				}
			}
		},
		handleTouchEnd () {/*实现字母滑动定位效果*/
			this.touchStatus = true
		}
	}
}
</script>

<style lang="stylus" scoped="">
@import '~styles/varibles.styl'
.list
	display:flex
	flex-direction :column /*flex垂直居中*/
	justify-content:center /*flex垂直居中*/
	position: absolute
	top: 1.58rem
	right: 0
	bottom: 0
	width: .4rem
	.item
		text-align:center
		line-height: .4rem
		color:$bgColor
		font-size: .24rem
</style>